﻿using UnityEngine;
using System.Collections;

public class MuzzleFlashAnimate : MonoBehaviour {

	public float minScale = 0.5f;
	public float maxScale = 1.5f;

	void UpdateScaleAndRotation(){
		float scaleValue = Random.Range (minScale, maxScale);
		transform.localScale = Vector3.one * scaleValue;

		Vector3 localEulerAngle = transform.localEulerAngles;
		localEulerAngle.z = Random.Range (0.1f, 90.0f);
		transform.localEulerAngles = localEulerAngle;

		/*
		Quaternion localRotation = transform.localRotation;

		transform.localRotation = localRotation;
		*/
	}

	public void InitMuzzleFlash(){
		gameObject.SetActive (false);
	}

	public void ActiveMuzzleFlash(float activeTime){
		gameObject.SetActive (true);
		StartCoroutine (ActiveMuzzleFlashProcess (activeTime));
	}

	IEnumerator ActiveMuzzleFlashProcess(float activeTime){
		float deltaTime = 0.0f;
		while(deltaTime < activeTime){
			deltaTime += Time.deltaTime;
			UpdateScaleAndRotation();
			yield return new WaitForEndOfFrame();
		}
		InitMuzzleFlash ();
	}
}
